const Category = require("../models/category");
const formidable = require('formidable');
const fs = require("fs");
const path = require("path");
const uuid = require("node-uuid");
const { returnFn } = require("../config/returnFn");

const checkRootLogin = (req, res, next) => {
    if (!req.session.employee) {
        return returnFn(res, 400, "未登录");
    }
    next();
}

const queryTopCategory = (req, res) => {
    Category.queryTopCategory((err, data) => {
        if (err) return returnFn(res, 503, "数据库异常");

        let obj = {
            total: data.length,
            rows: data
        }
        res.send(obj);
    })
}
const querySecondCategory = (req, res) => {
    Category.querySecondCategory(req.query.id, (err, data) => {
        if (err) return returnFn(res, 503, "数据库异常");

        var obj = {
            total: data.length,
            rows: data
        }
        res.send(obj);
    })
}

const queryTopCategoryPaging = (req, res) => {
    let page = {
        page: req.query.page ? parseInt(req.query.page) : 0,
        size: req.query.size ? parseInt(req.query.size) : 10
    }
    Category.queryTopCategoryPaging(page, (err1, rows) => {
        if (err1) return returnFn(res, 503, "数据库异常");

        Category.countTopCategory((err2, result) => {
            if (err2) return returnFn(res, 503, "数据库异常");

            page.count = result.count;
            page.rows = rows;
            returnFn(res, 200, "success", page);
        })
    })
}
const updateTopCategory = (req, res) => {
    let category = new Category({
        id: req.body.id ? req.body.id : "",
        categoryName: req.body.categoryName ? req.body.categoryName : "",
        isDelete: req.body.isDelete ? req.body.isDelete : ""
    });
    Category.updateTopCategory(category, (err, result) => {
        if (err) return returnFn(res, 503, "数据库异常");
        returnFn(res, 200, "success");
    })
}
const addTopCategory = (req, res) => {
    let category = new Category({
        categoryName: req.body.categoryName ? req.body.categoryName : ""
    });
    Category.addTopCategory(category, (err, result) => {
        if (err) return returnFn(res, 503, "数据库异常");
        returnFn(res, 200, true);
    })
}
const querySecondCategoryPaging = (req, res) => {
    let page = {
        page: req.query.page ? parseInt(req.query.page) : 1,
        size: req.query.size ? parseInt(req.query.size) : 10
    }
    Category.querySecondCategoryPaging(page, (err1, rows) => {
        if (err1) return returnFn(res, 503, "数据库异常");

        Category.countSecondCategory((err2, result) => {
            if (err2) return returnFn(res, 503, "数据库异常");

            page.count = result.count;
            page.rows = rows;
            returnFn(res, 200, "success", page);
        })
    })
}
const addSecondCategoryPic = (req, res) => {
    // 创建表单上传
    let form = new formidable.IncomingForm();
    // 设置编辑
    form.encoding = 'utf-8';
    // 设置文件存储路径
    form.uploadDir = "public/upload/brand";
    // 保留后缀
    form.keepExtensions = true;
    // 设置单文件大小限制 2m
    form.maxFieldsSize = 2 * 1024 * 1024;
    // form.maxFields = 1000;  设置所以文件的大小总和
    form.parse(req, (err, fields, files) => {
        let file = files.file;

        let picName = uuid.v1() + path.extname(file.name);
        fs.rename(file.path, "public\\upload\\brand\\" + picName, (err) => {
            if (err) return returnFn(res, 502, "图片保存异常");
            let picAddr = "/upload/brand/" + picName;
            returnFn(res, 200, "success", { picAddr });
        })
    })

}

module.exports = {
    checkRootLogin,
    queryTopCategory,
    querySecondCategory,
    queryTopCategoryPaging,
    updateTopCategory,
    addTopCategory,
    querySecondCategoryPaging,
    addSecondCategoryPic
}